// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import ELEMENT from 'element-ui'
import  base from './config'
import echarts from 'echarts';
import Breadcrumb from '@/components/common/Breadcrumb'
// import 'element-ui/lib/theme-chalk/index.css'
Vue.component('Breadcrumb', Breadcrumb)
Vue.config.productionTip = false

window.bus = new Vue({})

window.commo = {
  accountInformation: {},
  hotelInformation: {},
  token: sessionStorage.token,
  companyId: sessionStorage.companyId,
  defaultParameter: {
    timestamp: 0,
    version: 'string',
    operation: 'string'
  },
  uuid: sessionStorage.getItem('uuid'),
  qiniuUrl: 'http://upload-z2.qiniu.com',
  qiniuImageUrl: 'http://ozjs8a6dq.bkt.clouddn.com/',
  url: 'http://192.168.10.253:8089/',
  time: () => {
    const date = new Date()
    let year = date.getFullYear()
    let mouth = date.getMonth() + 1
    let day = date.getDate()
    if (mouth > 12) mouth = 1
    let time = year + '-' + mouth + '-' + day
    return time
  },
  showMenu: (obj) => {
    const Menu = JSON.parse(sessionStorage.Menu)
    Menu.forEach(item => {
      obj[item.functionId] = item.functionId
    })
  }
}

const messageInformation = ELEMENT.Message

axios.defaults.timeout = 20000
let {baseApi} = base;

axios.defaults.baseURL = baseApi
axios.interceptors.request.use(config => {
  return config
}, function (error) {
  // loading.close()
  return Promise.reject(error)
})

axios.interceptors.response.use(res => {

  if (res.data.errCode) {
    if (res.data.errCode !== 0) {
      messageInformation.error({
        message: '出错了 ' + res.data.errMsg
      })
      if (res.data.errCode === 10001) {
        router.push({path: '/'})
      }
    }
  } else {
    if (res.status) {
      if (res.status !== 200) {
        messageInformation.error({
          message: '出错了 '
        })
      }
    }
  }
  return res
}, function (error) {
  return Promise.reject(error)
})
Vue.prototype.$echarts = echarts
Vue.prototype.$http = axios
window.$http = axios

Vue.config.productionTip = false

Vue.use(ELEMENT)

window.fetch = function (options) {
  return window.$http({
    method: options.method || 'GET',
    url: options.url,
    params: {
      token: window.commo.token
    },
    data: Object.assign({}, window.commo.defaultParameter, options.data)
  })
}

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})
